import { defineStore } from 'pinia';
import { ref } from 'vue';

export const useForumStore = defineStore('forum', () => {
  const API_URL = "https://fc-mp-d7d620be-76ea-43bb-a05a-d38ceb13c655.next.bspapp.com/comment";
  
  // 状态
  const posts = ref([]);
  const currentPost = ref(null);
  const loading = ref(false);
  
  // 获取帖子列表
  const fetchPosts = async () => {
    loading.value = true;
    try {
      const response = await uni.request({
        url: API_URL,
        method: 'POST',
        data: {
          action: 'getPosts',
          page: 1,
          pageSize: 20
        }
      });
      
      if (response.data.status === 1) {
        posts.value = response.data.data.list || [];
      }
    } catch (error) {
      console.error('获取帖子失败:', error);
      uni.showToast({
        title: '获取帖子失败',
        icon: 'none'
      });
    } finally {
      loading.value = false;
    }
  };

  // 获取帖子详情
  const fetchPostDetail = async (id) => {
    loading.value = true;
    try {
      const response = await uni.request({
        url: API_URL,
        method: 'POST',
        data: {
          action: 'getPostDetail',
          postId: id
        }
      });
      
      if (response.data.status === 1) {
        currentPost.value = response.data.data;
      }
    } catch (error) {
      console.error('获取帖子详情失败:', error);
      uni.showToast({
        title: '获取帖子详情失败',
        icon: 'none'
      });
    } finally {
      loading.value = false;
    }
  };

  // 点赞帖子
  const likePost = async (postId) => {
    try {
      const response = await uni.request({
        url: API_URL,
        method: 'POST',
        data: {
          action: 'likePost',
          postId: postId
        }
      });
      
      if (response.data.status === 1) {
        // 更新本地状态
        const post = posts.value.find(p => p._id === postId);
        if (post) {
          post.islinked = !post.islinked;
          post.likes = post.islinked ? (post.likes || 0) + 1 : (post.likes || 0) - 1;
        }
        
        if (currentPost.value?._id === postId) {
          currentPost.value.islinked = !currentPost.value.islinked;
          currentPost.value.likes = currentPost.value.islinked ? 
            (currentPost.value.likes || 0) + 1 : 
            (currentPost.value.likes || 0) - 1;
        }
      }
    } catch (error) {
      console.error('点赞失败:', error);
      uni.showToast({
        title: '点赞失败',
        icon: 'none'
      });
    }
  };

  // 添加评论
  const addComment = async (postId, commentData) => {
    try {
      const response = await uni.request({
        url: API_URL,
        method: 'POST',
        data: {
          action: 'addComment',
          postId: postId,
          commentData: commentData
        }
      });
      
      if (response.data.status === 1) {
        // 更新本地状态
        if (currentPost.value?._id === postId) {
          if (!currentPost.value.comments) {
            currentPost.value.comments = [];
          }
          currentPost.value.comments.push(commentData);
          currentPost.value.comment = (currentPost.value.comment || 0) + 1;
        }
      }
    } catch (error) {
      console.error('评论失败:', error);
      uni.showToast({
        title: '评论失败',
        icon: 'none'
      });
    }
  };

  return {
    posts,
    currentPost,
    loading,
    fetchPosts,
    fetchPostDetail,
    likePost,
    addComment
  };
}); 